package com.example.exercise.app.feedback.service.impl;

import com.example.exercise.app.feedback.mapper.entity.AppFeedbackEntity;
import com.example.exercise.app.feedback.mapper.entity.AppFeedbackReplyEntity;
import com.example.exercise.app.feedback.dto.request.AppFeedbackRequest;
import com.example.exercise.app.feedback.dto.request.MgFeedbackRequest;
import com.example.exercise.app.feedback.dto.response.AppFeedbackResponse;
import com.example.exercise.app.feedback.dto.response.FeedbackDetail;
import com.example.exercise.app.feedback.mapper.AppFeedbackMapper;
import com.example.exercise.app.feedback.mapper.AppFeedbackReplyMapper;
import com.example.exercise.app.feedback.service.AppFeedbackService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class AppFeedbackServiceImpl implements AppFeedbackService {

	@Resource
	private AppFeedbackMapper feedbackMapper;
	@Resource
	private AppFeedbackReplyMapper feedbackReplyMapper;

	@Override
	public int insertAppFeedback(AppFeedbackRequest feedbackRequest){
		AppFeedbackEntity feedbackEntity = new AppFeedbackEntity();
		BeanUtils.copyProperties(feedbackRequest,feedbackEntity);
		return feedbackMapper.insertAppFeedback(feedbackEntity);
	}

	@Override
	public List<FeedbackDetail> getReplyInfos(int userId) {
		List<FeedbackDetail> list = new ArrayList<>();
		List<AppFeedbackEntity> feedbackEntities = feedbackMapper.findByUserId(userId);
		for (AppFeedbackEntity feedbackEntity : feedbackEntities) {
			FeedbackDetail userFeedbackReplyResponse = new FeedbackDetail();
			userFeedbackReplyResponse.setFeedbackEntity(feedbackEntity);
			List<AppFeedbackReplyEntity> feedbackReplyEntities = feedbackReplyMapper.findByFeedbackId(feedbackEntity.getId());
			userFeedbackReplyResponse.setFeedbackReplyEntities(feedbackReplyEntities);
			list.add(userFeedbackReplyResponse);
		}
		return list;
	}

	@Transactional(isolation = Isolation.REPEATABLE_READ)
	@Override
	public void saveFeedbackReplyInfo(int replyType, int id, String context, String adminId) {
		if(replyType == 1){
			feedbackMapper.replyInc(id);
		}else if(replyType == 2){
			feedbackMapper.ignoreInc(id);
		}
		AppFeedbackReplyEntity feedbackReplyEntity = new AppFeedbackReplyEntity();
		feedbackReplyEntity.setReplyType(replyType);
		feedbackReplyEntity.setReplyContext(context);
		feedbackReplyEntity.setFeedbackId(id);
		feedbackReplyEntity.setAdminId(adminId);
		feedbackReplyMapper.insertAppFeedbackReply(feedbackReplyEntity);
	}

	@Transactional(readOnly = true)
	@Override
	public boolean[] canFeedback(int id) {
		AppFeedbackEntity feedbacEntity = feedbackMapper.findById(id);
		Integer replyCount = feedbacEntity.getReplyCount();
		boolean boolIgnore = true;
		boolean boolReply = true;
		if(replyCount > 0 || replyCount == -1){
			boolIgnore = false;
		}
		if(replyCount > 5 || replyCount == -1){
			boolReply = false;
		}
		return new boolean[]{boolReply,boolIgnore};
	}

	@Transactional(readOnly = true)
	@Override
	public Object[] getFeedbackList(MgFeedbackRequest feedbackRequest) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		Integer count = feedbackMapper.getCountByWhere(feedbackRequest);
		int page = feedbackRequest.getCurrentPage();
		int pageSize = feedbackRequest.getPageSize();
		feedbackRequest.setBegin((page - 1) * pageSize);
		List<AppFeedbackEntity> list = feedbackMapper.findByWhere(feedbackRequest);
		List<AppFeedbackResponse> feedbackResponses = new ArrayList<>();
		for (AppFeedbackEntity feedbackEntity : list) {
			AppFeedbackResponse feedbackResponse = new AppFeedbackResponse();
			BeanUtils.copyProperties(feedbackEntity,feedbackResponse);
			feedbackResponse.setCreateTime(sdf.format(feedbackEntity.getCreateTime()));
			feedbackResponses.add(feedbackResponse);
		}
		return new Object[]{count,feedbackResponses};
	}

	@Transactional(readOnly = true)
	@Override
	public FeedbackDetail getDetail(int id) {
		FeedbackDetail feedbackDetail = new FeedbackDetail();
		AppFeedbackEntity feedbackEntity = feedbackMapper.findById(id);
		if(feedbackEntity != null){
			feedbackDetail.setFeedbackEntity(feedbackEntity);
			int feedbackId = feedbackEntity.getId();
			List<AppFeedbackReplyEntity> feedbackReplyEntities = feedbackReplyMapper.findByFeedbackId(feedbackId);
			feedbackDetail.setFeedbackReplyEntities(feedbackReplyEntities);
		}
		return feedbackDetail;
	}

}